'''
装备管理相关页面测试
'''
from Lib.base.page_object.car_manager.car_add import CarAdd
from Lib.base.page_object.equipment_manager.car_equipment.car_equipment_add import CarEquipment
from Lib.base.page_object.equipment_manager.car_equipment.car_equipment_list import CarEquipmentList
from Lib.base.page_object.car_manager.car_list import CarList
from Lib.base.page_object.equipment_manager.single_equipment.single_equipment_add import SingleEquipmentAdd
from Lib.base.page_object.equipment_manager.single_equipment.single_equipment_list import SingleEquipmentList
from Lib.base.page_object.equipment_manager.other_equipment.other_equipment_add import OtherEquipmentAdd
from Lib.base.page_object.equipment_manager.other_equipment.other_equipment_list import OtherEquipmentList
from selenium.webdriver.common.by import By
import allure
import os
from Lib.common.assert_class import AssertClass
import pytest


@pytest.fixture(scope='class', autouse=True)
@pytest.mark.usefixtures('login')
def set_cls(login):
    TestEquipment.car = CarAdd(login)
    TestEquipment.car_list = CarList(login)
    TestEquipment.assert_class = AssertClass(login)
    TestEquipment.equipmentAdd = CarEquipment(login)
    TestEquipment.equipmentList = CarEquipmentList(login)


@allure.feature("装备管理页面测试")
class TestEquipment:

    @allure.story('车载装备管理')
    @allure.title('新增车载装备')
    def test_01_add_equipment(self):
        '''
        新增一个车载装备
        :return:
        '''
        with allure.step('新增关联的车辆'):
            # 新增关联的车辆
            self.car.car_add()
        # 新增车载装备
        with allure.step("新增车载装备"):
            self.equipmentAdd.car_equipment_add()
        # 设置断言
        assert_args = (By.XPATH, '//p[contains(text(), "创建成功")]')
        self.assert_class.make_assert_by_text(args=assert_args, assert_data='创建成功', context='创建车载装备')

    @allure.story('车载装备管理')
    @allure.title('车载装备列表筛选')
    def test_02_equipment_list(self):
        '''
        查询车载装备列表
        :return:
        '''
        self.equipmentList.car_equipment_list()
        # 设置断言
        assert_args = (By.XPATH, '//table[@class="rz-table__body"]//p[text()="自动化设备名称"]')
        self.assert_class.make_assert_by_text(args=assert_args, assert_data='自动化设备名称', context="查询车载装备列表")

    @allure.story('车载装备管理')
    @allure.title('车载装备搜索并查看详情')
    def test_03_equipment_search_detail(self):
        '''
        搜索车载装备并查看详情
        :return:
        '''
        self.equipmentList.car_equipment_search_detail()
        # 设置断言
        assert_args = (By.XPATH, '//span[text()="车载装备详情"]')
        self.assert_class.make_assert_by_text(args=assert_args, assert_data='车载装备详情', context="查看车载装备详情")

    @allure.story('车载装备管理')
    @allure.title('车载装备搜索并编辑')
    def test_04_equipment_search_update(self):
        '''
        搜索车载装备并编辑
        :return:
        '''
        self.equipmentList.car_equipment_search_update()
        # 设置断言
        assert_args = (By.XPATH, '//p[text()="更新成功!"]')
        self.assert_class.make_assert_by_text(args=assert_args, assert_data='更新成功!', context="查看车载装备详情")

    @allure.story('车载装备管理')
    @allure.title('车载装备搜索并删除')
    def test_05_equipment_search_delete(self):
        '''
        搜索车载装备并删除
            :return:
            '''
        self.equipmentList.car_equipment_search_delete()
        # 设置断言 获取删除弹窗
        assert_args = (By.XPATH, '//p[contains(text(), "删除失败,该车载设备已安装")]')
        text = self.equipmentList.get_text(args=assert_args, context="删除弹窗")
        # 关联车辆无法删除
        if "删除失败,该车载设备已安装至车辆" in text:
            # 删除车辆 自动删除设备
            self.car_list.car_detail_delete()
            assert_args = (By.XPATH, '//p[contains(text(), "暂无数据")]')
            with allure.step("删除设备绑定的车辆"):
                self.assert_class.make_assert_by_text(args=assert_args, assert_data="暂无数据", context="删除车辆")
        else:
            with allure.step("直接删除设备"):
                self.assert_class.make_assert_by_text(args=assert_args, assert_data="暂无数据", context="删除车辆")


if __name__ == '__main__':
    pytest.main(['-s', '-v', './equipment_test.py::TestEquipment'])
    # pytest.main(['-v', './vehicle_equipment_test.py', '--alluredir', '../result', '--clean-alluredir'])
    # os.system(r'copy ..\config\environment.properties ..\result\environment.properties')
    # os.system('allure generate  ../result -o ../report/ --clean')
